package com.good.msgsys.controller;

import com.good.msgsys.dto.PageResult;
import com.good.msgsys.dto.Result;
import com.good.msgsys.dto.ResultCode;
import com.good.msgsys.entity.Project;
import com.good.msgsys.exception.CommonException;
import com.good.msgsys.service.ProjectService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author junlong
 * @date 2021/1/4 17:20
 */
@Api(tags = "项目模块")
@RestController
@RequestMapping("/api/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    // 新增项目
    @ApiOperation("新增项目")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public Result<Project> add(@ApiParam(value = "项目实体", required = true) @RequestBody Project project) throws CommonException {
        if (!StringUtils.isEmpty(project.getUserId())) {
            projectService.add(project);
            return new Result<>(ResultCode.SUCCESS, project);
        }
        throw new CommonException(ResultCode.USER_ID_EMPTY);
    }

    // 根据id删除项目(逻辑删除)
    @ApiOperation("根据id删除项目")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result<Object> deleteById(@ApiParam(value = "项目id", required = true) @PathVariable("id") String id) {
        projectService.deleteById(id);
        return new Result<>(ResultCode.SUCCESS);
    }

    // 更新项目
    @ApiOperation("更新项目")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result<Project> update(@ApiParam(value = "项目id", required = true) @PathVariable("id") String id, @ApiParam(value = "项目实体", required = true) @RequestBody Project project) {
        project.setId(id);
        projectService.update(project);
        return new Result<>(ResultCode.SUCCESS);
    }

    // 根据id查询项目
    @ApiOperation("根据id查询项目")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result<Project> findById(@ApiParam(value = "项目id", required = true) @PathVariable("id") String id) {
        Project project = projectService.findById(id);
        return new Result<>(ResultCode.SUCCESS, project);
    }

    // 查询所有项目
    @ApiOperation("查询所有项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码，从0开始", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "Integer", defaultValue = "5"),
            @ApiImplicitParam(name = "userId", value = "用户id，查询用户的所有项目是需要传递该字段（没有该字段则表示查询所有用户的所有项目）", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "项目状态：0未提交立项申请，1已提交，待立项审核，2已立项，3已提交结项申请，待审核，4项目通过，5项目不通过，6已删除(默认查询所有状态的项目)", dataType = "String")
    })
    @RequestMapping(value = "", method = RequestMethod.GET)
    public Result<PageResult<Project>> findAll(
            @RequestParam("page") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit,
            @RequestParam(value = "userId", required = false) String userId,
            @RequestParam(value = "state", required = false) Integer state
    ) {
        PageResult<Project> projectPage = null;
        if (!StringUtils.isEmpty(userId)) {
            projectPage = projectService.findAll(userId, page, limit, state);
        } else {
            projectPage = projectService.findAll(page, limit, state);
        }
        return new Result<>(ResultCode.SUCCESS, projectPage);
    }

    // 员工修改项目状态(提交、申请结项)
    @ApiOperation("员工修改项目状态(提交、申请结项)")
    @RequestMapping(value = "/apply", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true),
            @ApiImplicitParam(name = "state", value = "项目状态：1提交，3提交结项申请", required = true)
    })
    public Result<Object> apply(@RequestParam(value = "projectId") String projectId, @RequestParam(value = "state") Integer state) throws CommonException {
        if (state != 1 && state != 3) {
            throw new CommonException(ResultCode.PARAM_ERROR);
        }
        projectService.apply(projectId, state);
        return new Result<>(ResultCode.SUCCESS);
    }

    // 主管修改项目状态(立项审核，结项审核)
    @ApiOperation("主管修改项目状态(立项审核，结项审核)")
    @RequestMapping(value = "/examine", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required = true),
            @ApiImplicitParam(name = "state", value = "项目状态：2立项通过，4结项通过，5不通过", required = true)
    })
    public Result<Object> findAll(@RequestParam(value = "projectId") String projectId, @RequestParam(value = "state") Integer state) throws CommonException {
        if (state != 2 && state != 4 && state != 5) {
            throw new CommonException(ResultCode.PARAM_ERROR);
        }
        projectService.examine(projectId, state);
        return new Result<>(ResultCode.SUCCESS);
    }
}
